/*
 *  This file is part of Spidex.
 *  
 *  Spidex is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Spidex is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Spidex.  If not, see <http://www.gnu.org/licenses/>.
 */

package spidex.posterSearcher;

import java.util.List;
import javax.swing.JTree;
import javax.swing.SwingWorker;
import javax.swing.tree.DefaultMutableTreeNode;
import spidex.FilmInfoInterface;

/**
 *
 * @author xkucer11
 */
public class CSFDPosterSearcherResultWorker extends 
        SwingWorker<List<FilmInfoInterface>,Void> {
    private PosterSearcher searcher;
    private String name;
    private DefaultMutableTreeNode topNode;
    private JTree resultTree;
    private List<FilmInfoInterface> result;
    public CSFDPosterSearcherResultWorker(
            PosterSearcher searcher, DefaultMutableTreeNode topNode) {
        this.searcher = searcher;
        this.topNode = topNode;
    }
    @Override
    protected List<FilmInfoInterface> doInBackground() throws Exception {
        DefaultMutableTreeNode category = null;
        System.out.println("Thread started");
        if(name == null) {
            System.out.println("Name is null");
            return null;
        }
        this.result = searcher.findFilm( name );
        

        if(result.size() < 1) {
            return null;
        }

        for(int index=0; index < result.size(); index++) {
            category = 
                new DefaultMutableTreeNode(result.get(index).getTitle());
            topNode.add(category);
        }
        return result;
    }
    
    protected void done() {
        System.out.println("Worker finished");
    }
    
    public void setName(String name) {
        this.name = name;
    }
            

}
